// pages/voucherRecive/voucherRecive.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId:'',
    skuCode:'',
    skuName:'',
    createTime:'',
    addresslist:[],
    color:'#A3A4AA',
    btnDisable:true,
    phone:'',
    background1:"url('https://sx-manage.oss-cn-hangzhou.aliyuncs.com/pic/radio-2.jpg') no-repeat 0rpx 4rpx",
    background2:"url('https://sx-manage.oss-cn-hangzhou.aliyuncs.com/pic/radio-1.jpg') no-repeat 0rpx 4rpx",
    currentId:'',
    currentAddress:'',
    consigneePhone:'',
    consigneeName:'',
  },
  //添加地址
  addAdress:function(){
    let orderId=this.data.orderId;
    let skuCode=this.data.skuCode;
    let skuName=this.data.skuName;
    let createTime=this.data.createTime;
    wx.navigateTo({
      url: '/pages/voucherAddress/voucherAddress?orderId='+orderId+'&skuCode='+skuCode+'&skuName='+skuName+'&createTime='+createTime
    })
  },
  //点击地址选中
  checkAddress:function(e){
    let arrry=e.currentTarget.dataset.id.split(',');
    this.setData({
      currentId:arrry[0],
      consigneeName:arrry[1],
      consigneePhone:arrry[2],
      currentAddress:arrry[3],
    })
    console.log(this.data.currentId);
    console.log(this.data.currentAddress);
    console.log(this.data.consigneeName);
    console.log(this.data.consigneePhone);
  },
//领取
recevice:function(){
  console.log(this.data.currentId)
  console.log(this.data.currentAddress)
  let id=this.data.currentId;
  let currentAddress=this.data.currentAddress;
  let consigneePhone=this.data.consigneePhone;
  let consigneeName=this.data.consigneeName;
  let orderId=this.data.orderId;
  let _this=this;
  if(id>0){
    console.log(9999,id);
    //领取
    wx.request({
      url:app.globalData.onlineUrl+"/redemption/receive",
      header:{'content-type': 'application/json'},
      method:"POST",
      data:{consigneeId:id,consigneeAddress:currentAddress,consigneePhone:consigneePhone,consigneeName:consigneeName,id:orderId},
      dataType:"json",
      complete:function(result) {
        wx.showToast({
          title: "领取成功",
          icon: 'success',
        });
        _this.authorizationBtn();
      }
    })
  }else{
    wx.showToast({
      title: "请选择或添加收货地址",
      icon: 'none',
    });
  }
},
  //  // 是否设置过授权
  //  openPopup() {
  //   console.log(123);
  //   const _this = this;
  //   // 获取用户的当前设置，判断是否点击了“总是保持以上，不在询问”
  //   wx.getSetting({
  //     withSubscriptions: true, // 是否获取用户订阅消息的订阅状态，默认false不返回
  //     success(res) {
  //       console.log('res.authSetting', res.authSetting)
  //       if (res.authSetting['scope.subscribeMessage']) {
  //         console.log('用户点击了“总是保持以上，不再询问”')
  //          _this.authorizationBtn();
  //       } else {
  //         console.log('用户点击“总是保持以上，不再询问”则每次都会调起订阅消息')
  //         //因为没有选择总是保持，所以需要调起授权弹窗再次授权
  //           _this.authorizationBtn();
  //       }
  //       wx.switchTab({
  //         url: '/pages/order/order'
  //       })
  //     },
  //   })
  // },

    // 授权
  authorizationBtn() {
    wx.requestSubscribeMessage({
      tmplIds: ['9cfuuyH99tleq365J0uTFoOEdHOk-Kldi-kZoKzr7mI'],
      success(res) {
        console.log('授权成功',res)
        wx.switchTab({
          url: '/pages/order/order'
        })
      }
    })
  },
  //查询地址列表
  getAdressList: function () {
    console.log(this.data.phone);
    let _this=this;
    console.log(12233);
    wx.request({
      url:app.globalData.onlineUrl+"/consignee/list",
      header:{'content-type': 'application/json'},
      method:"POST",
      data:{phone:_this.data.phone},
      dataType:"json",
      complete:function(result) {
        console.log(result.data.data);
        let currentId=-1;
        let currentAddress='';
        let consigneePhone='';
        let consigneeName='';
        if(result.data.data.length!=0){
          currentId=result.data.data[0].id;
          currentAddress=result.data.data[0].province+''+result.data.data[0].city+''+result.data.data[0].county+''+result.data.data[0].detailAddress;
          consigneePhone=result.data.data[0].phone;
          consigneeName=result.data.data[0].name;
        }
          _this.setData({
            addresslist:result.data.data,
            currentId:currentId,
            currentAddress:currentAddress,
            consigneePhone:consigneePhone,
            consigneeName:consigneeName,
            btnDisable:currentId==-1?true:false
          })
          console.log(6666,_this.data.currentId);
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    this.setData({
      orderId:options.orderId,
      skuCode:options.skuCode,
      skuName:options.skuName,
      createTime:options.createTime
    })
    console.log(this.data.createTime);
    console.log(this.data.orderId,this.data.skuCode);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      phone:wx.getStorageSync('phone')
    })
    this.getAdressList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    console.log(12242);
    return {
      title: '分享标题',
      path: '/pages/index',
      // imageUrl: '/images/share.png'
    }
  }
})